package com.h.heal.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.User;
import com.h.heal.entity.Video;
import com.h.heal.mapper.VideoMapper;
import com.h.heal.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<Video> findAll() {
        return videoMapper.findAll();
    }

    @Override
    public void deleteById(int id) {
        videoMapper.deleteById(id);
    }

    @Override
    public List<Video> findByvideo_vname(String video_vname) {
        return videoMapper.findByvideo_vname(video_vname);
    }

    @Override
    public List<Video> findByvideo_uid(String video_uid) {
        return videoMapper.findByvideo_uid(video_uid);
    }

    @Override
    public String create(Video video) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        video.setVideo_timestamp(simpleDateFormat.format(date));
        videoMapper.create(video);
        return "ok";
    }

    @Override
    public Video findById(int id) {
        return videoMapper.findById(id);
    }

    @Override
    public String edit(Video video) {
        try {
            videoMapper.edit(video);
            return "ok";
        } catch (Exception e) {
            System.out.println("VideoServiceImpl.edit() ERROR:e=" + e);
        }
        return "error";

    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String video_vname = request.getParameter("video_vname");
        String video_uid = request.getParameter("video_uid");


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        videoMapper.findBySearch(video_vname, video_uid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());


        myPageInfo.setSearch1(video_vname);
        myPageInfo.setSearch2(video_uid);

        return myPageInfo;
    }


    @Override
    public MyPageInfo findBySearchUser(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String video_vname = request.getParameter("video_vname");
        String video_uid = String.valueOf(((User) request.getSession().getAttribute("user_info")).getUser_uid());


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        videoMapper.findBySearch(video_vname, video_uid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());




        return myPageInfo;
    }
}
